const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: app.data.navHeight,
    cardsList:[],
    loading:true,
    select:null,
    /* 支付相关字段 */ 
    showContact: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.data.jsonPage) {
      var nvabarData = {
        title: app.data.jsonPage.title,
        bgclase: false,
        showCapsule: true,
        textclase: false,
        texttype: false
      };
      this.setData({
        nvabarData: nvabarData,
      });
    } else {
      var left = this;
      app.getjsonPage = res => {
        var nvabarData = {
          title: res.openingVip.title,
          bgclase: false,
          showCapsule: true,
          textclase: false,
          texttype: false
        };
        left.setData({
          nvabarData: nvabarData
        });
      };
    }
    this.getList();
  },
  /* 获取卡列表 */ 
  getList(){
    wx.$.fetch('api/cards',{
      hideLoading:false,
      method:'GET',
      data:{
        channel: wx.$.channel,
        recommend_id: wx.$.recommend_id,
      }
    }).then(res=>{
      console.log(res)
      this.setData({
        content_html: res.data.data[0].desc,
        cardsList:res.data.data
      })
    })
  },
  /* wxParse回调 */
  handleWxparseRendered: function () {
    setTimeout(() => {
      wx.hideLoading();
    }, 200);
    this.setData({
      loading: false
    });
  },
  /* 选择会员卡 */ 
  btnChoice(e){
     let id = e.currentTarget.dataset.id;
     let index = e.currentTarget.dataset.index;
     if(this.data.select == id){
       this.setData({
          content_html: this.data.cardsList[0].desc,
          select:null
       })
     }else{
      this.setData({
        content_html: this.data.cardsList[index].desc,
        select:id
       })
     }
  },
  /* 购买会员 */
  getPurchaseVip(){
      if(!this.data.select){
        wx.showToast({
          title: '请选择要购买的会员卡',
          icon: 'none',
          duration: 1000
        })
      }else{
        console.log("购买")
        this.buthis()
      }
  }, 
  /* 调起支付接口 */ 
  buthis() {
    var _that = this
    const system = wx.getSystemInfoSync().system;
    if (false) {//system.indexOf("iOS") != -1
      var buyInfo = {
        goods_id: 3,
        goods_type: 'buy',
        quantity: 1
      };
      buyInfo = JSON.stringify(buyInfo);
      this.setData({
        buyInfo: buyInfo,
        showContact: true,
        ios: true
      })
    } else {
      wx.$.fetch(`api/order/checkout`, {
        method: 'post',
        data: {
          card_id: this.data.select,
          recommend_id: wx.$.recommend_id,
          channel: wx.$.channel,
        }
      }).then(res => {
        console.log(res)
        if (res.data.code == 200) {
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              setTimeout(function () {
                wx.reLaunch({
                  url: "/pages/home/home"
                });
              }, 200)
            }
          })
        }else{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1000
            })
        }
      })
    }
  },
  /* ios关闭提示 */  
  closeContact() {
    this.setData({
      showContact: false
    })
  },
  noHandler() {
    return false
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.$.recommend_id = 0;
    wx.$.channel = '';
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: "pages/home/home",
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    };
  }
})